import lejos.pc.comm.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class BTRemote implements ActionListener {

  DataInputStream in;
  DataOutputStream out;
  JButton tRead, write, stop;
  boolean reading = true, writing = false, stoping = false;

  public BTRemote(String btname) throws Exception {
    NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
    NXTInfo[] nxtInfo = nxtComm.search(btname);
    nxtComm.open(nxtInfo[0]);
    JFrame window = new JFrame(btname);
    window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    window.setLayout(new GridLayout(11,2));
    JTextField light, dark, speed, rotate, setp, kp, result, bf, basespeed;
    window.add(new JLabel("light"));
    light = new JTextField();
    window.add(light);
    window.add(new JLabel("dark"));
    dark = new JTextField();
    window.add(dark);
    window.add(new JLabel("basespeed"));
    basespeed = new JTextField();
    window.add(basespeed);
    window.add(new JLabel("speed"));
    speed = new JTextField();
    window.add(speed);
    window.add(new JLabel("rotate"));
    rotate = new JTextField();
    window.add(rotate);
    window.add(new JLabel("setp"));
    setp = new JTextField();
    window.add(setp);
    window.add(new JLabel("bf"));
    bf = new JTextField();
    window.add(bf);
    window.add(new JLabel("kp"));
    kp = new JTextField();
    window.add(kp);
    window.add(new JLabel("result"));
    result = new JTextField();
    window.add(result);
    window.setVisible(true);
    tRead = new JButton("stop reading");
    window.add(tRead);
    write = new JButton("write");
    window.add(write);
    stop = new JButton("stop");
    window.add(stop);
    tRead.addActionListener(this);
    write.addActionListener(this);
    stop.addActionListener(this);
    window.pack();
    in = new DataInputStream(nxtComm.getInputStream());
    out = new DataOutputStream(nxtComm.getOutputStream());
    while (true) {
      if (reading) {
	out.writeInt(1);
	out.flush();
	light.setText(""+in.readInt());
	dark.setText(""+in.readInt());
	basespeed.setText(""+in.readInt());
	speed.setText(""+in.readInt());
	rotate.setText(""+in.readInt());
	setp.setText(""+in.readDouble());
	bf.setText(""+in.readDouble());
	kp.setText(""+in.readDouble());
	result.setText(""+in.readDouble());
      }
      if (writing) {
	out.writeInt(2);
	out.flush();
	out.writeInt(Integer.parseInt(light.getText()));
	out.writeInt(Integer.parseInt(dark.getText()));
	out.writeInt(Integer.parseInt(basespeed.getText()));
	out.writeInt(Integer.parseInt(speed.getText()));
	out.writeInt(Integer.parseInt(rotate.getText()));
	out.writeDouble(Double.parseDouble(setp.getText()));
	out.writeDouble(Double.parseDouble(bf.getText()));
	out.writeDouble(Double.parseDouble(kp.getText()));
	out.writeDouble(Double.parseDouble(result.getText()));
	out.flush();
	writing = false;
      }
      if (stoping) {
	out.writeInt(3);
	out.flush();
	break;
      }
    }
    System.exit(0);
  }
  
  public void actionPerformed( ActionEvent e ) {
    if (e.getSource() == tRead) {
      reading = !reading;
      if (reading)
	tRead.setText("stop reading");
      else
	tRead.setText("start reading");
    }
    else if (e.getSource() == write) {
      writing = true;
    }
    else if (e.getSource() == stop) {
      stoping = true;
    }
  }
    
  public static void main(String args[]) throws Exception {
    BTRemote btr = new BTRemote("tweety10");
  }
  
}